package com.yssw.task;

import com.yssw.common.core.redis.RedisCache;
import com.yssw.onlinechat.domain.OnlineChatUser;
import com.yssw.onlinechat.service.impl.WebSocketServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.AbstractMap;
import java.util.Map;

/**
 * @Author KnockingFarmers
 * @Date 2023/4/22 10:58
 * @PackageName:com.yssw.task
 * @ClassName: SaveChatRecords
 * @Description: TODO
 * @Version 1.0
 */
@Component("saveChatRecordsTask")
@Slf4j
public class SaveChatRecordsTask {

    @Autowired
    RedisCache redisCache;

    /**
     * 保存聊天记录到redis
     */

    public void save() {
        log.info("================>save");
        boolean onlineChatUserMap = redisCache.deleteObject("onlineChatUserMap");
        if (onlineChatUserMap){
            log.info("删除聊天记录成功");
        }
        log.info("=============>{}","保存聊天记录中....");
        redisCache.setCacheMap("onlineChatUserMap", WebSocketServiceImpl.userMap);
        log.info("保存完成=======>{}",redisCache.getCacheMap("onlineChatUserMap"));
    }

    /**
     * 删除聊天记录
     */

    public void clear() {
        boolean onlineChatUserMap = redisCache.deleteObject("onlineChatUserMap");
        if (onlineChatUserMap){
            log.info("删除聊天记录成功");
        }
    }
    public Map<String, OnlineChatUser> get() {
        return redisCache.getCacheMap("onlineChatUserMap");
    }

}
